<template>
    <view>
		<view v-if="(yxformplus_data || null != null) && yxformplus_data.length > 0" class="bg-white">
			<view class="form-container">
				<view class="padding-main oh">
					<block v-if="msgtype == 'lvshi'">
						<view class="radius bg-white padding-main cr-main spacing-mb">次数暂时无用，备用，您当前正在向律师发起留言咨询</view>
					</block>
					<block v-else-if="msgtype == 'wanshan'">
						<view class="radius bg-white padding-main cr-main spacing-mb">完善房源信息</view>
					</block>
					<view v-if="(form_info || null) != null" class="radius bg-white padding-main cr-main spacing-mb">
						<mp-html :content="form_info.description" />
					</view>
					<!-- 自定义表单数据展示 自定义一个方法，用来从组件中返回操作-->
					<view class="padding-horizontal-main radius spacing-mb spacing-mt">
						<component-tj-form ref="tjform" :propData="yxformplus_data" :propEditorPath="editor_path_type" propFormType="single" @backtosubmitform="onSubmitone" @backtoeditischoseaddress="editischoseaddress"></component-tj-form>
					</view>

					<view class="form-gorup form-gorup-submit">
						<button class="cr-white round text-size" :style="'background:'+diytheme.main_color+';border:1px solid '+diytheme.main_color+';'" type="default" form-type="submit" hover-class="none" :loading="form_submit_loading" :disabled="form_submit_loading" @tap="tijiao">提交</button>
					</view>
				</view>
			</view>
		</view>
        <view v-else>
            <!-- 提示信息 -->
            <component-no-data :propStatus="data_list_loding_status"></component-no-data>
        </view>
    </view>
</template>
<script>
    const app = getApp();
    import componentNoData from "@/components/no-data/no-data";
	import componentTjForm from "../tj-form-uv/tj-form-uv";
	import {system_base_info} from "@/common/js/common/common";
    export default {
        data() {
            return {
				yxform_use_what_style: app.globalData.data.yxform_use_what_style || 1,
                data_list_loding_status: 1,
                data_list_loding_msg: '处理错误',
                form_submit_loading: false,
				params:null,
				nickname:'',
				mobile:'',
				msgtype:'',
				order_id:0,
				data_bottom_line_status: false,
				// 自定义表单插件
				form_id:0,
				xu_hao:0,//第几次提交该表单
				yxformplus_data:null,
				editor_path_type:'',
				user_location_cache_key: app.globalData.data.cache_userlocation_key,
				form_info: null,
				diytheme: null,
				lang_info: null,
				share_info: null,
            };
        },

        components: {
            componentNoData,
			componentTjForm
        },
        props: {},
		
		onShow() {
		    app.globalData.page_event_onshow_handle();
		},
        onLoad(params) {
			//params['id']=1;
			var msgtype = params.msgtype || null;
			if(msgtype != null)
			{
				this.setData({
				    params: params,
					msgtype:msgtype
				});
			}else{
				this.setData({
				    params: params
				});
			}
			var order_id = params.order_id || null;
			if(order_id != null)
			{
				this.setData({
				    order_id: order_id,
				});
			}
			
			this.init();
		},

        onShow() {
			var self = this;
			setTimeout(function () {
				self.user_location_init();
			}, 1500);
        },

        methods: {
			// 加载之前
			init() {
				// 先获取语言基础信息
				system_base_info()
				.then(data=>{
					this.init_before(data)
					.then(()=>{
						this.init_real();
					});
				});
			},
			
			// 设置本页参数
			init_before(data){
				var self = this;
				return new Promise((resolve, reject) => {
					self.setData({
						lang_info: data.system_lang_info,
						diytheme: data.diytheme,
					});
					/*uni.setNavigationBarTitle({
					    title: data.index_page_title
					});*/
					resolve();
				});
			},
			// 获取数据
			init_real() {
				this.get_data();
			},
			
			// 地址信息初始化
			user_location_init() {
			    var result = uni.getStorageSync(this.user_location_cache_key) || null;
			    var data = null;
			    if (result != null) {console.log(this.$refs);
					// 直接调用子组件的方法，初始化子组件中的地址信息
					this.$refs.tjform.user_location_init();
			    }
			},
			
			// 子组件中的地址信息初始化完毕，会执行这里，重新加载一下数据
			editischoseaddress(){
				this.init();//直接重新加载该页面获取最新值
			},
			
			// 点击提交按钮，调用组件内的表单检测方法，其方法完毕后会返回调用本页面组件自定义方法，进行继续提交
			tijiao(){
				//通过组件定义的ref调用..方法
				this.$refs.tjform.PluginsyxformplusCheck();
			},
			
			// 组件检测完毕，成功后，返回到页面执行这里，进行正式的表单提交
			onSubmitone(){
				uni.showLoading({
					title: '提交中...'
				});
				this.setData({
					form_submit_loading: true
				});
				var self = this;
				
				var data = {};
				data['form_id'] = self.form_id || 0;
				data['xu_hao'] = self.xu_hao || 0;
				data['order_id'] = self.order_id || 0;
				//if(parseInt(self.form_id) == 2){
					data['is_delete_temp'] = 0;//不删除临时数据
				//}
				
				// 网络请求
				uni.request({
					url: app.globalData.get_request_url("singlesave", "index", "yxformplus"),
					method: 'POST',
					data: data,
					dataType: 'json',
					success: res => {console.log(res);
						uni.hideLoading();
						if (res.data.code == 0) {
							app.globalData.showToast(res.data.msg, "success");
							
							if((self.params.from || null) != null && self.params.from == 'goods-detail')
							{
								setTimeout(function() {
									app.globalData.url_open("/pages/goods-detail/goods-detail?id="+self.params.goods_id);
								}, 2000);
							}else{
								setTimeout(function() {
									app.globalData.url_open("/pages/fcc/formplus/dangan-list/dangan-list?id="+self.form_id);
								}, 2000);
							}
							/*if(parseInt(self.form_id) == 1){
								setTimeout(function() {
									// 跳转到buy去直接下个单
									var form_id = res.data.data.form_id || 0;//本次是哪个表单
									var xu_id = res.data.data.xu_id || 0;//该会员第几次保存本表单
									if(parseInt(form_id)==0 && parseInt(xu_id)==0)
									{
										// 此情况不会触发
										app.globalData.url_open("/pages/dangan-list/dangan-list", true);
									}else{
										var spec = [];
										var data = {
										    "buy_type": "goods",
										    "goods_id": 1,//1号特殊商品
										    "stock": 1,
										    "spec": JSON.stringify(spec),
											"form_id":form_id,
											"xu_id":xu_id
										};
										app.globalData.url_open('/pages/buy/buy?data=' + encodeURIComponent(JSON.stringify(data)));
									}
								}, 2000);
							}else{
								// 这里当做房源信息完善，回到委托订单去
								app.globalData.url_open("/pages/fcc/serv/order/order", true);
							}*/
						} else {
							this.setData({
								form_submit_loading: false
							});
							if (app.globalData.is_login_check(res.data)) {
								app.globalData.showToast(res.data.msg);
							} else {
								app.globalData.showToast('提交失败，请重试！');
							}
						}
					},
					fail: () => {
						uni.hideLoading();
						this.setData({
							form_submit_loading: false
						});
						app.globalData.showToast(this.$t('common.internet_error_tips'));
					}
				});
			},

			// 初始化数据
			get_data(){
				var self = this;
				uni.showLoading({
				    title: this.$t('common.loading_in_text'),
				});
				this.setData({
				    data_list_loding_status: 1
				});
				uni.request({
				    url: app.globalData.get_request_url("detail", "index", "yxformplus"),
				    method: 'POST',
				    data: {
				        id: this.params.id || 0,
				    },
				    dataType: 'json',
				    success: res => {
				        uni.hideLoading();
				        uni.stopPullDownRefresh();//console.log(res);
				        if (res.data.code == 0) {
				            var data = res.data.data;
							// 是否需要登录
							let form_info = ((data.form_info || null) != null) ? data.form_info : null;
							let is_single_login = ((form_info.is_single_login || null) != null) ? parseInt(form_info.is_single_login) : 0;
							//console.log('---->'+is_single_login);
							if(is_single_login == 1)
							{
								var user = app.globalData.get_user_cache_info() || null;
								if (user == null) {
									app.globalData.get_user_info_redict(this, 'get_data');
									return false;
								}
							}
							var form_title = ((data.form_info || null) != null) ? (data.form_info.title || '') : '';
				            self.setData({
				                yxformplus_data: data.singledata || null,
								editor_path_type: data.editor_path_type || '',
								form_id: data.form_id || 0,
								xu_hao: data.xu_hao || 0,
								form_info: form_info,
				                data_list_loding_status: 3,
				                data_bottom_line_status: true,
				                data_list_loding_msg: '',
								// 基础自定义分享
								share_info: {
									title: form_info.seo_title || form_title,
									kds: form_info.seo_keywords || form_title,
									desc: form_info.seo_desc || form_title,
									path: '/pages/fcc/formplus/dangan-form/dangan-form',
									img: form_info.icon || '',
									query: 'id=' + this.params.id
								}
				            });
							
							if(form_title != '')
							{
								setTimeout(function() {
								    uni.setNavigationBarTitle({
								    	title: form_title
								    });
								}, 500);
							}
							// 分享菜单处理
							app.globalData.page_share_handle(self.share_info);
				        } else {
				            self.setData({
				                data_list_loding_status: 2,
				                data_bottom_line_status: false,
				                data_list_loding_msg: res.data.msg
				            });
				            if (app.globalData.is_login_check(res.data, self, 'init')) {
				                app.globalData.showToast(res.data.msg);
				            }
				        }
				    },
				    fail: () => {
				        uni.hideLoading();
				        uni.stopPullDownRefresh();
				        self.setData({
				            data_list_loding_status: 2,
				            data_bottom_line_status: false,
				            data_list_loding_msg: this.$t('common.internet_error_tips')
				        });
				        app.globalData.showToast(this.$t('common.internet_error_tips'));
				    }
				});
			}
        }
    };
</script>
<style>
</style>